package integral

import (
	"context"
	"strconv"
	"strings"

	"mall/common/globalkey"
	"mall/common/jwt"
	"mall/common/resultx"
	"mall/common/utils/pointy"
	"mall/service/forum/api/internal/svc"
	"mall/service/forum/api/internal/types"
	"mall/service/forum/api/internal/utils/parse"
	"mall/service/user/rpc/userclient"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetUserTransactionListLogLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 获取用户交易日志
func NewGetUserTransactionListLogLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserTransactionListLogLogic {
	return &GetUserTransactionListLogLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetUserTransactionListLogLogic) GetUserTransactionLogList(req *types.UserTransactionLogListReq) (resp *types.UserTransactionLogListResp, err error) {
	page, pageSize := parse.ParsePageInfo(
		req.Current,
		req.PageSize,
		l.svcCtx.Config.Query.DefaultAmount,
		l.svcCtx.Config.Query.MaxAmount,
	)

	var filters []string
	userId := jwt.GetUserId(l.ctx)
	if userId == 0 {
		return nil, resultx.NewErrCode(resultx.TOKEN_EXPIRE_ERROR)
	}
	filters = append(filters, "UserId,equalTo:"+strconv.FormatInt(int64(userId), 10))
	assetType := globalkey.UserAssetTypePoint
	if req.AssetType != nil && *req.AssetType > 0 {
		assetType = *req.AssetType
	}
	if req.ChangeType != nil {
		filters = append(filters, "ChangeType,equalTo:"+strconv.FormatInt(int64(*req.ChangeType), 10))
	}
	filters = append(filters, "AssetType,equalTo:"+strconv.FormatInt(int64(assetType), 10))
	var preload string = "userTransactionLog:Id,UserId,UserName,AssetType,ChangeType,ChangeAmount,OldAmount"
	preload += ",SourceType,SourceId,Desc,Status,CreatedAt,UpdatedAt"
	var eagerLoad string = "userTransactionLog:"

	var sorter *string
	if req.Sorter != nil {
		sorter = pointy.GetPointer(strings.TrimSpace(*req.Sorter))
	}
	var platformId uint32 = globalkey.ThirdPartyPlatformWechatMini
	res, err := l.svcCtx.UserRpc.GetUserTransactionLogList(l.ctx, &userclient.BaseListReq{
		Offset:     uint32((page - 1) * pageSize),
		Limit:      uint32(pageSize),
		Preload:    pointy.GetPointer(preload),
		EagerLoad:  pointy.GetPointer(eagerLoad),
		Filter:     pointy.GetPointer(strings.Join(filters, ";")),
		Sorter:     sorter,
		PlatformId: pointy.GetPointer(platformId),
	})
	if err != nil {
		return nil, err
	}

	var hasNextPage bool
	var hasPreviousPage bool
	if res.HasNextPage != nil && *res.HasNextPage > 0 {
		hasNextPage = true
	}
	if res.HasPreviousPage != nil && *res.HasPreviousPage > 0 {
		hasPreviousPage = true
	}
	resp = &types.UserTransactionLogListResp{
		BaseListInfo: types.BaseListInfo{
			Total:           res.Total,
			Current:         page,
			PageSize:        pageSize,
			HasNextPage:     hasNextPage,
			HasPreviousPage: hasPreviousPage,
		},
	}
	if len(res.List) > 0 {
		for _, v := range res.List {
			resp.List = append(resp.List, &types.UserTransactionLogInfo{
				Id:           v.Id,
				UserId:       v.UserId,
				UserName:     v.UserName,
				AssetType:    v.AssetType,
				ChangeType:   v.ChangeType,
				ChangeAmount: v.ChangeAmount,
				OldAmount:    v.OldAmount,
				SourceType:   v.SourceType,
				SourceId:     v.SourceId,
				Desc:         v.Desc,
				Status:       v.Status,
				CreatedAt:    v.CreatedAt,
				UpdatedAt:    v.UpdatedAt,
			})
		}
	}
	return resp, nil
}
